<?php
class User extends AppModel {

        var $name = 'User';
        var $useDbConfig = 'users';
        var $hasOne = array(
			'Profile'=>array(
				'foreignKey'=>'user_id',
				'fields'=>'',
			),
			'UsersSoctype',
			'Privacy',
			//'SearchOption'
	);

        var $hasMany = array(
                        'Foto' => array(
					'className' => 'Foto',
					'foreignKey' => 'user_id',
					'dependent' => false,
					//'conditions' => array('status >'=>1),
					'fields' => array('id', 'hash', 'alt', 'path', 'ext', 'form'),
					'order' => 'Foto.status DESC',
					'limit' => '',
					'offset' => '',
					'exclusive' => '',
					'finderQuery' => '',
					'counterQuery' => ''
                        ),
        );

	var $validate = array(
		'login'=>array(
			array('rule' => 'alphaNumeric', 'message' => 'разрешены только латинские буквы и цифры'),
			array('rule' => 'isUnique', 'message' => 'такой логин уже существует'),
			array('rule' => array('between', 3, 20), 'message'=>'от 3 до 20 символов'),
		),
		'password'=>array(
		//	array('rule' => VALID_NOT_EMPTY, 'message'=>'введите пароль'),
	//		array('rule' => array('minLength', 6), 'message'=>'мин. 6 символов')
		),
		'email'=>array(
			array('rule' => array('_isUnique', 'email'), 'message'=>'токая почтa уже существует'),
			array('rule' => array('email'), 'message'=>'формат почты не подходит'),
			//array('rule' => VALID_NOT_EMPTY, 'message' => 'введите почту'),
		),
		'captcha' => array(
			array('rule' => 'checkCaptcha', 'message' => 'не верный код')
		),
		'newpassword'=>array(
		//	array('rule' => VALID_NOT_EMPTY, 'message'=>'введите пароль'),
			array('rule' => array('minLength', 6), 'message'=>'мин. 6 символов')
		),
		'repassword'=>array(
		//	array('rule' => VALID_NOT_EMPTY, 'message'=>'введите пароль'),
			array('rule' => array('minLength', 6), 'message'=>'мин. 6 символов'),
			array('rule' => array('compare', 'newpassword'), 'message'=>'пароли не одинаковы'),
		),
		
	);

	function getData($uid) {
		$c = array('User.id'=>$uid);
		return $this->find('first', array('conditions' => $c, 'recursive'=>2));
	}

	function getByTypes($types = array()) {
		return $this->find('all', array('conditions'=>$conditions));
	}	

	function compare($data, $field) {
		return (current($data) == $this->data[$this->name][$field]);
	}

	function getSocType($uid) {
		$this->unbindModel(array('hasOne'=>array('Profile')));
		$c = array('user_id'=>$uid);
		return $this->find('first', array('conditions'=>$c));
	}

	function loginUuid($login = null, $uuid = null) {
		$data = false;
		if ($login && $uuid) {
			$this->unBindModel(array('hasOne'=>array('Profile')));
			$c = array('login'=>$login);
			$f = array('id', 'login', 'email', 'uuid');
			$u = $this->find('first', array('conditions'=>$c, 'fields'=>$f));

			if ($u['User']['uuid'] === $uuid) {
				$data = $u['User'];
			}
		}
		return $data;
	}

	function checkPass($uid = null, $pass = null) {
		return $this->find('first', array('conditions'=>array('User.id'=>$uid, 'User.passwd'=>$pass), 'recursive'=>-1));
	}

	function userListConditions($uData) {
		$uCond['User.mailconfirm'] = 0;

		if (!empty($uData['Profile']['sex'])) {
			$sex = $uData['Profile']['sex'];
			if ($sex === '1') {
				$uCond['Profile.sex'] = 2;
			} else {
				$uCond['Profile.sex'] = 1;
			}
		}

		return $uCond;
	}


}
?>
